﻿using System;
using System.Web.Mvc;
using EzDesk.Apps.Model.Project;
using EzDesk.Clients.DesktopWeb.ProjectService;
using EzDesk.Utility.Extensions;
using EzDesk.Utility.ServiceStatusInfo;
using EzDesk.Utility.Web.Controllers;
using EzDesk.Utility.Web.ServiceReference;

namespace EzDesk.Clients.DesktopWeb.Controllers
{
    public class ProjectController : EzDeskAppController
    {
        static IProjectService ProjectService
        {
            get { return ServiceReferenceFactory<IProjectService, ProjectServiceClient>.GetService(); }
        }

        #region [ Page Methods ]
        [HttpGet]
        public ActionResult DailyWork()
        {
            return View();
        }

        [HttpGet]
        public ActionResult WorkitemList()
        {
            return View();
        }

        [HttpGet]
        public ActionResult AddWrokitem()
        {
            ViewData["CurrentUser"] = EzApiClient.GetSimpleUserById(CurrentUserId);
            ViewData["ProjectList"] = null;
            return View();
        }

        [HttpGet]
        public ActionResult Workitem(Guid id)
        {
            var statusInfo = ProjectService.GetWorkitem(id);

            if (statusInfo.Status == ServiceStatus.Success)
            {
                return View(
                    new
                        {
                            WorkItem = statusInfo.Result,
                        });
            }
            // todo: 定义通用的出错页面
            throw new Exception(statusInfo.Info);
        }

        #endregion

        #region [ Ajax Methods ]

        [HttpPost]
        public JsonResult DoAddWorkItem(
            string title,  // 任务内容
            string description,  // 任务备注（详细描述）
            int? estimatePoint  // 预估时间数
            )
        {
            //todo: 对参数进行判错

            var workitem = new Workitem
                    {
                        OwnerId = CurrentUserId,
                        CreatorId = CurrentUserId,
                        CreateOn = DateTime.Now,
                        Title = title,
                        Description = description,
                    };
            var statusInfo = ProjectService.AddWorkitem(workitem);

            if (statusInfo.Status == ServiceStatus.Success)
            {
                statusInfo.Info = string.Format("添加任务“{0}”完成", title);
                statusInfo.Data = new
                                      {
                                          workitemId = statusInfo.Result
                                      };
            }
            return Json(statusInfo);
        }

        #endregion
    }

}
